package com.shop.loan.model.view;

import com.shop.loan.handler.*;
import com.shop.loan.handler.DiscountsHandler;
import com.shop.loan.model.*;
import com.shop.loan.model.vo.*;
import com.shop.loan.proxy.LoanOrderByNewProxy;
import com.shop.loan.util.LoanOrderUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import xyz.erupt.annotation.Erupt;
import xyz.erupt.annotation.EruptField;
import xyz.erupt.annotation.expr.ExprBool;
import xyz.erupt.annotation.sub_erupt.*;
import xyz.erupt.annotation.sub_field.*;
import xyz.erupt.annotation.sub_field.sub_edit.*;
import xyz.erupt.jpa.model.BaseModel;
import xyz.erupt.upms.handler.ViaMenuValueCtrl;
import xyz.erupt.upms.model.EruptUser;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Set;

@Entity
@Table(name = "s_loan_order")
@Erupt(
        name = "借款订单表",
        power = @Power(add = false, edit = false, delete = false),
        orderBy = "id desc",
        dataProxy = LoanOrderByNewProxy.class,
        filter = @Filter("LoanOrderByNew.orderNum = 1 and LoanOrderByNew.isDelete = false"),
        rowOperation = {
                @RowOperation(
                        title = "用户详情",
                        icon = "fa fa-user",
                        mode = RowOperation.Mode.SINGLE,
                        show = @ExprBool(
                                exprHandler = ViaMenuValueCtrl.class,
                                params = "userDetail"  //权限标识，菜单类型为按钮，类型值为testBtn即可控制该按钮
                        ),
                        tpl = @Tpl(
                                path = "/tpl/userDetail.html",
                                width = "50%",
                                engine = Tpl.Engine.Thymeleaf
                        ),
                        type = RowOperation.Type.TPL
                ),
                @RowOperation(
                        title = "审核操作",
                        icon = "fa fa-check-square-o",
                        mode = RowOperation.Mode.SINGLE,
                        eruptClass = LoanOrderAuthVo.class,
                        operationHandler = OrderSignCreateHandler.class,
                        ifExpr = "item.signStatus == '待生成' && item.status == '审核中'",
                        ifExprBehavior = RowOperation.IfExprBehavior.HIDE
                ),
                @RowOperation(
                        title = "查看合同",
                        icon = "fa fa-gavel",
                        mode = RowOperation.Mode.SINGLE,
                        operationHandler = SeeContractHandler.class,
                        ifExpr = "item.signStatus == '已签署'",
                        ifExprBehavior = RowOperation.IfExprBehavior.HIDE
                ),
                @RowOperation(
                        title = "重置还款密码",
                        icon = "fa fa-hand-o-up",
                        mode = RowOperation.Mode.BUTTON,
                        tpl = @Tpl(
                                path = "/tpl/reset_pay_password.html",
                                engine = Tpl.Engine.Thymeleaf
                        ),
                        type = RowOperation.Type.TPL
                ),
                @RowOperation(
                        title = "设置优惠金额",
                        icon = "fa fa-gg",
                        mode = RowOperation.Mode.SINGLE,
                        eruptClass = DiscountsVo.class,
                        operationHandler = DiscountsHandler.class,
                        show = @ExprBool(
                                exprHandler = ViaMenuValueCtrl.class,
                                params = "Discounts"  //权限标识，菜单类型为按钮，类型值为testBtn即可控制该按钮
                        ),
                        ifExpr = "item.status == '待还款'",
                        ifExprBehavior = RowOperation.IfExprBehavior.HIDE
                ),
                @RowOperation(
                        title = "还款",
                        icon = "fa fa-cny",
                        mode = RowOperation.Mode.SINGLE,
                        eruptClass = PayOrderByOfflineVo.class,
                        operationHandler = OfflinePayHandler.class,
                        show = @ExprBool(
                                exprHandler = ViaMenuValueCtrl.class,
                                params = "PayOrderByPayment"  //权限标识，菜单类型为按钮，类型值为testBtn即可控制该按钮
                        ),
                        ifExpr = "item.status == '待还款'",
                        ifExprBehavior = RowOperation.IfExprBehavior.HIDE
                ),

        },
        drills = {
                @Drill(code = "bill_drill",
                        title = "分期账单",
                        link = @Link(linkErupt = LoanOrderBill.class,  //关联表
                                joinColumn = "orderId")),  //关联表达式
                @Drill(code = "pay_bill",
                        title = "还款记录",
                        icon = "fa fa-paypal",
                        link = @Link(linkErupt = LoanOrderPayLogByNoAuth.class,  //关联表
                                joinColumn = "orderId")),  //关联表达式
                @Drill(code = "third_pay_log",
                        title = "三方支付记录",
                        icon = "fa fa-cc-paypal",
                        link = @Link(linkErupt = PayOrderThirdByNoAuth.class,  //关联表
                                joinColumn = "orderId")),  //关联表达式
        }
)
@SQLDelete(sql = "update s_loan_order set is_delete = true, delete_time = now() where id = ?")
@Getter
@Setter
public class LoanOrderByNew extends BaseModel {

    @Transient
    @EruptField(
            edit = @Edit(title = "基本信息", type = EditType.DIVIDE)
    )
    private String divide_base;

    @EruptField(
            views = @View(title = "id", width = LoanOrderUtil.orderListByIdWidth),
            edit = @Edit(
                    title = "id",
                    readonly = @Readonly,
                    search = @Search
            )
    )
    @Column(name = "id", insertable = false, updatable = false)
    private Long orderId;


    @Transient
    @EruptField(
            edit = @Edit(
                    title = "手机号码",
                    show = false,
                    search = @Search
            )
    )
    private String userPhone;

    @Transient
    @EruptField(
            edit = @Edit(
                    title = "姓名",
                    show = false,
                    search = @Search
            )

    )
    private String userName;

//    @ManyToOne
//    @EruptField(
//            views = @View(title = "借款用户", column = "name", type = ViewType.SAFE_TEXT, width = LoanOrderUtil.orderListByUserWidth),
//            edit = @Edit(
//                    title = "借款用户",
//                    type = EditType.REFERENCE_TREE
//            )
//    )
//    @JoinColumn(name = "user_id")
//    private User user;

    @EruptField(
            views = @View(title = "用户id", show = false)
    )
    @Column(name = "user_id")
    private Long userId;

    @Transient
    @EruptField(
            views = @View(title = "借款用户", type = ViewType.SAFE_TEXT, width = LoanOrderUtil.orderListByUserWidth)
    )
    private String user_name;


    @ManyToOne
    @EruptField(
            views = @View(title = "渠道", column = "name", width = LoanOrderUtil.orderListByChannelWidth),
            edit = @Edit(
                    title = "渠道",
                    search = @Search(vague = true),
                    type = EditType.REFERENCE_TREE
            )
    )
    @JoinColumn(name = "channel_id")
    private Channel channel;

    @EruptField(
            edit = @Edit(
                    title = "渠道是否显示"
            )
    )
    private Boolean channelStatistics = true;

    @EruptField(
            views = @View(title = "订单状态", width = LoanOrderUtil.orderListByStatusWidth),
            edit = @Edit(
                    title = "订单状态",
                    type = EditType.CHOICE,
                    search = @Search(),
                    choiceType = @ChoiceType(
                            vl = {
                                    @VL(label = "审核中", value = "1"),
                                    @VL(label = "审核拒绝", value = "2"),
                                    @VL(label = "待签署", value = "3"),
                                    @VL(label = "待放款", value = "4"),
                                    @VL(label = "放款驳回", value = "5"),
                                    @VL(label = "待还款", value = "7"),
                                    @VL(label = "已结束", value = "10"),
                            }
                    ))
    )
    private Integer status;


    @EruptField(
            views = @View(title = "电子签状态", width = LoanOrderUtil.orderListBySignStatusWidth),
            edit = @Edit(
                    title = "电子签状态",
                    type = EditType.CHOICE,
                    search = @Search(),
                    choiceType = @ChoiceType(
                            vl = {
                                    @VL(label = "待生成", value = "99"),
                                    @VL(label = "等待签署", value = "0"),
                                    @VL(label = "签署中", value = "1"),
                                    @VL(label = "已签署", value = "2"),
                                    @VL(label = "过期", value = "3"),
                                    @VL(label = "拒签", value = "4"),
                                    @VL(label = "作废", value = "6"),
                                    @VL(label = "撤销", value = "7"),
                            }
                    ))
    )
    private Integer signStatus = 99;

    @ManyToOne
    @EruptField(
            views = @View(title = "审核客服", column = "name", width = LoanOrderUtil.orderListByServiceUserWidth),
            edit = @Edit(
                    title = "审核客服",
                    type = EditType.REFERENCE_TREE,
                    search = @Search,
                    ifRender = @ExprBool(exprHandler = SuperAdminHandler.class)
            )
    )
    @JoinColumn(name = "auth_service_id")
    private EruptUser authServiceUser;


    @ManyToOne
    @EruptField(
            views = @View(title = "放款客服", column = "name", width = LoanOrderUtil.orderListByServiceUserWidth),
            edit = @Edit(
                    title = "放款客服",
                    type = EditType.REFERENCE_TREE,
                    search = @Search,
                    ifRender = @ExprBool(exprHandler = SuperAdminHandler.class)
            )
    )
    @JoinColumn(name = "loan_service_id")
    private EruptUser loanServiceUser;


    @ManyToOne
    @EruptField(
            views = @View(title = "售后客服", column = "name", width = LoanOrderUtil.orderListByServiceUserWidth),
            edit = @Edit(
                    title = "售后客服",
                    type = EditType.REFERENCE_TREE,
                    search = @Search,
                    ifRender = @ExprBool(exprHandler = SuperAdminHandler.class)
            )
    )
    @JoinColumn(name = "after_service_id")
    private EruptUser afterServiceUser;


    @ManyToOne
    @EruptField(
            views = @View(title = "催收客服", column = "name", width = LoanOrderUtil.orderListByServiceUserWidth),
            edit = @Edit(
                    title = "催收客服",
                    type = EditType.REFERENCE_TREE,
                    search = @Search,
                    ifRender = @ExprBool(exprHandler = SuperAdminHandler.class)
            )
    )
    @JoinColumn(name = "collection_service_id")
    private EruptUser collectionServiceUser;

    @Transient
    @EruptField(
            edit = @Edit(title = "订单金额", type = EditType.DIVIDE)
    )
    private String divide_money;

    @EruptField(
            views = @View(title = "借款金额", width = LoanOrderUtil.orderListByMoneyWidth),
            edit = @Edit(title = "借款金额", notNull = true)
    )
    private BigDecimal money;

    @EruptField(
            views = @View(title = "担保费", width = LoanOrderUtil.orderListByMoneyWidth),
            edit = @Edit(title = "担保费", notNull = true)
    )
    private BigDecimal shopMoney;

    @EruptField(
            views = @View(title = "利息金额", width = LoanOrderUtil.orderListByRateMoneyWidth),
            edit = @Edit(title = "利息金额")
    )
    private BigDecimal rateMoney = BigDecimal.ZERO;

    @EruptField(
            views = @View(title = "已还本息", width = LoanOrderUtil.orderListByPayMoneyWidth),
            edit = @Edit(title = "已还本息")
    )
    private BigDecimal payMoney = BigDecimal.ZERO;

    @EruptField(
            views = @View(title = "已还商品金额", width = LoanOrderUtil.orderListByPayMoneyWidth),
            edit = @Edit(title = "已还商品金额")
    )
    private BigDecimal payMoneyByGoods = BigDecimal.ZERO;

    @EruptField(
            views = @View(title = "优惠本息", width = LoanOrderUtil.orderListByDiscountsMoneyWidth),
            edit = @Edit(
                    title = "优惠本息",
                    readonly = @Readonly
            )
    )
    private BigDecimal discountsMoney = BigDecimal.ZERO;

    @EruptField(
            views = @View(title = "优惠商品金额", width = LoanOrderUtil.orderListByDiscountsMoneyWidth),
            edit = @Edit(
                    title = "优惠商品金额",
                    readonly = @Readonly
            )
    )
    private BigDecimal discountsMoneyByGoods = BigDecimal.ZERO;

    @Transient
    @EruptField(
            edit = @Edit(title = "其他信息", type = EditType.DIVIDE)
    )
    private String divide_other;

    @EruptField(
            views = @View(title = "第几单",
                    sortable = true, width = LoanOrderUtil.orderListByOrderNumWidth),
            edit = @Edit(
                    title = "第几单",
                    search = @Search,
                    readonly = @Readonly
            )
    )
    private Integer orderNum = 0;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    @EruptField(
            views = @View(title = "电子签记录"),
            edit = @Edit(
                    title = "电子签记录",
                    readonly = @Readonly,
                    type = EditType.TAB_TABLE_ADD
            )
    )
    @JoinColumn(name = "order_id")
    private Set<LoanOrderSignLog> orderSignLogs;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    @EruptField(
            views = @View(title = "附件信息"),
            edit = @Edit(
                    title = "附件信息",
                    type = EditType.TAB_TABLE_ADD
            )
    )
    @JoinColumn(name = "order_id")
    private Set<LoanOrderAttachment> attachments;


    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    @EruptField(
            views = @View(title = "审核记录"),
            edit = @Edit(
                    title = "审核记录",
                    readonly = @Readonly,
                    type = EditType.TAB_TABLE_ADD
            )
    )
    @OrderBy
    @JoinColumn(name = "order_id")
    private Set<LoanOrderAuthLog> orderAuthLogs;


//    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
//    @EruptField(
//            edit = @Edit(
//                    title = "合同协议",
//                    type = EditType.TAB_TABLE_ADD
//            )
//    )
//    @JoinColumn(name = "order_id")
//    @ApiModelProperty(name = "orderContracts", value = "合同协议", dataType = "string", example = "1")
//    private Set<LoanOrderContract> orderContracts;


    @EruptField(
            views = @View(title = "年利率(%)", width = LoanOrderUtil.orderListByYearRateWidth),
            edit = @Edit(title = "年利率(%)", notNull = true)
    )
    private BigDecimal yearRate;


    private Long bankId;

    @EruptField(
            views = @View(title = "期数", width = LoanOrderUtil.orderListByPeriodWidth),
            edit = @Edit(
                    title = "期数"
            )
    )
    private Integer period;

    @EruptField(
            views = @View(title = "每期天数", width = LoanOrderUtil.orderListByPeriodDayWidth),
            edit = @Edit(title = "每期天数")
    )
    private Integer periodDay;


    @Transient
    @EruptField(
            edit = @Edit(
                    title = "是否有还款",
                    type = EditType.CHOICE,
                    search = @Search(),
                    show = false,
                    choiceType = @ChoiceType(
                            vl = {
                                    @VL(label = "有还款", value = "1"),
                            }
                    ))
    )
    private Integer partPay;


    @Transient
    @EruptField(
            edit = @Edit(
                    title = "是否有优惠",
                    type = EditType.CHOICE,
                    search = @Search(),
                    show = false,
                    choiceType = @ChoiceType(
                            vl = {
                                    @VL(label = "有优惠", value = "1"),
                            }
                    ))
    )
    private Integer hasDiscounts;


    @EruptField(
            views = @View(title = "当前逾期", width = LoanOrderUtil.orderListByOverdueStatusWidth),
            edit = @Edit(
                    title = "当前逾期",
                    type = EditType.BOOLEAN,
                    search = @Search(),
                    boolType = @BoolType(
                            trueText = "正常",
                            falseText = "逾期"
                    )
            )
    )
    private Boolean overdueStatus = true;

    @EruptField(
            views = @View(title = "逾期天数", width = LoanOrderUtil.orderListByOverdueDayWidth),
            edit = @Edit(
                    title = "逾期天数"
            )
    )
    private Integer overdueDay;


    @EruptField(
            views = @View(title = "历史逾期", width = LoanOrderUtil.orderListByHisOverdueStatusWidth),
            edit = @Edit(
                    title = "历史逾期",
                    type = EditType.BOOLEAN,
                    search = @Search(),
                    boolType = @BoolType(
                            trueText = "正常",
                            falseText = "逾期"
                    )
            )
    )
    private Boolean hisOverdueStatus;

    @EruptField(
            views = @View(title = "下单定位", width = LoanOrderUtil.orderListByOrderLocationWidth),
            edit = @Edit(
                    title = "下单定位"
            )
    )
    private String locationAddress;

    @EruptField(
            views = @View(title = "下单ip", show = false),
            edit = @Edit(
                    title = "下单ip",
                    show = false
            )
    )
    private String orderIp;


    @EruptField(
            views = @View(title = "申请时间"),
            edit = @Edit(title = "申请时间",
                    search = @Search(vague = true),
                    readonly = @Readonly,
                    dateType = @DateType(type = DateType.Type.DATE_TIME))
    )
    private Date createTime;

    @EruptField(
            views = @View(title = "放款时间"),
            edit = @Edit(title = "放款时间",
                    readonly = @Readonly,
                    search = @Search(vague = true),
                    dateType = @DateType(type = DateType.Type.DATE_TIME))
    )
    private Date loanTime;

    @EruptField(
            views = @View(title = "结束时间", width = "20%"),
            edit = @Edit(title = "结束时间",
                    readonly = @Readonly,
                    dateType = @DateType(type = DateType.Type.DATE_TIME))
    )
    private Date finishTime;

    @Transient
    @EruptField(
            edit = @Edit(title = "到期时间",
                    readonly = @Readonly,
                    search = @Search(vague = true),
                    dateType = @DateType(type = DateType.Type.DATE_TIME))
    )
    private Date payTime;

    private Boolean isDelete = false;

    private Date deleteTime;

    private Boolean appShow = true;

    @Lob
    @EruptField(
            edit = @Edit(title = "备注", type = EditType.TEXTAREA)
    )
    private String remark;

}
